package com.wawei.seeds.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wawei.seeds.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by parker on 15/6/7.
 */
public class ChampionBiddingFragmentAdapter extends BaseAdapter{
    private Context context;
    private List<ChampionBiddingItem> dataset = new ArrayList<>(0);

    public ChampionBiddingFragmentAdapter(Context context) {
        this.context = context;
    }

    public void setDataset(List<ChampionBiddingItem> dataset) {
        this.dataset = dataset;
    }

    public void appendDataset(List<ChampionBiddingItem> oldDataset){
        dataset.addAll(oldDataset);
    }

    public void insertDataset(List<ChampionBiddingItem> newDataset){
        for(ChampionBiddingItem item : newDataset){
            dataset.add(0, item);
        }
    }

    @Override
    public int getCount() {
        return dataset.size();
    }

    @Override
    public Object getItem(int position) {
        return (getCount() == 0)? null : dataset.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(getCount() <= 0) return null;
        ViewHolder viewHolder;
        View view = convertView;
        if(null != view){
            viewHolder = (ViewHolder)view.getTag();
        }
        else{
            view = LayoutInflater.from(context).inflate(R.layout.fragment_champion_bidding_item, parent, false);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        }

        ChampionBiddingItem item = dataset.get(position);
        viewHolder.biddingTitle.setText(item.biddingTitle);
        viewHolder.biddingAmount.setText(item.biddingAmount);
        viewHolder.biddingPublishTime.setText(item.biddingPublishTime);
        viewHolder.biddingStatus.setText(item.biddingStatus);

        return view;
    }

    public static class ChampionBiddingItem {
        public String biddingTitle;
        public String biddingPublishTime;
        public String biddingAmount;
        public String biddingStatus;
    }

    static class ViewHolder {
        @InjectView(R.id.biddingTitle)TextView biddingTitle;
        @InjectView(R.id.biddingPublishTime)TextView biddingPublishTime;
        @InjectView(R.id.biddingAmount)TextView biddingAmount;
        @InjectView(R.id.biddingStatus)TextView biddingStatus;

        public ViewHolder(View view){
            ButterKnife.inject(this, view);
        }
    }
}
